package com.loveprogrammer.base.websocket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName NioWebSocketServer
 * @Description websocket 服务
 * @Author admin
 * @Date 2024/3/4 11:28
 * @Version 1.0
 */
@Slf4j
public class NioWebSocketServer {

    private void start(){
        log.info("正在启动Websocket服务器>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>:");
        NioEventLoopGroup boss = new NioEventLoopGroup(1);
        NioEventLoopGroup work = new NioEventLoopGroup(10);

        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(boss,work);
            bootstrap.channel(NioServerSocketChannel.class);
            bootstrap.childHandler(new NioWebSocketChannelInitializer());
            Channel channel = bootstrap.bind(9091).sync().channel();

            channel.closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.error("Websocket服务器运行异常",e);
        } finally {
            boss.shutdownGracefully();
            work.shutdownGracefully();
            log.info("Websocket服务器已关闭>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        }

    }

    public static void main(String[] args) {
        new NioWebSocketServer().start();
    }
}
